package com.gaber.optional;

import com.gaber.stream.Java8Tester;

import java.util.Optional;

/**
 * @author gaber
 * @date 2023/8/2 14:09:38
 * @description TODO
 */
public class java8Tester {
    public static void main(String[] args) {
        Integer value1 = null;
        Integer value2 = new Integer(10);

        Optional<Integer> a = Optional.ofNullable(value1);
        System.out.println(a.toString());

        Optional<Integer> b = Optional.of(value2);
        System.out.println(b.get());
        System.out.println(sum(a, b));
    }

    public static Integer sum(Optional<Integer> a, Optional<Integer> b){

        // Optional.isPresent - 判断值是否存在
        System.out.println("第一个参数值存在: " + a.isPresent());
        System.out.println("第二个参数值存在: " + b.isPresent());

        // Optional.orElse - 如果值存在，返回它，否则返回默认值
        // Integer value1 = a.orElse(new Integer(0));
        Integer value1 = a.orElseGet(() -> value3()); //返回的是方法

        //Optional.get - 获取值，值需要存在
        Integer value2 = b.get();
        return value1 + value2;
    }

    // 返回 3
    public static Integer value3() {
        return 3;
    }
}
